#!/usr/bin/env python

from PyQt4 import QtGui as qg
from PyQt4 import QtCore as qc
from PyQt4.uic  import loadUi

class GoUi(qg.QMainWindow):
	""" This is the main ui, showing the camera and buttons and so on"""

	def __init__(self):
		qg.QMainWindow.__init__(self)
		loadUi("go.ui", self) #xml ui from designer
		#set up our vars
		self.inGame=0
		self.paused=0
		#a little hack to align our image correctly - why won't qt designer do this?
		self.gridlayout.setAlignment(self.display, qc.Qt.AlignCenter)
		#load a dummy board image
		self.image=qg.QPixmap("example.jpg")
		self.display.setPixmap(self.image)
		self.display.setFixedSize(self.image.size())
		#hook up signals&slots
		self.connect(self.conf, qc.SIGNAL("clicked()"), self.slotConf)
		self.connect(self.start, qc.SIGNAL("clicked()"), self.slotStartEnd)
		self.connect(self.pause, qc.SIGNAL("clicked()"), self.slotPause)

	def slotConf(self):
		""" Begin board calibration - finding the four corners"""
		self.label.setText("Please click on the 4 corners of the board")
		self.corners = []
		self.display.installEventFilter(self)
		self.conf.setEnabled(0) #you have to finish this one before you try again

	def eventFilter(self, obj, event):
		""" This is an event filter to pick up mouseclicks on our image
		obj --  the object the event happened to
		event -- the event. this could be some kind of mouseclick or keypress
		"""
		if obj == self.display:
			if event.type() == qc.QEvent.MouseButtonRelease:
				self.handleClick(event.pos())
				return 1 #we want to handle this
			else:
				return 0 #not wanted at all
		else:
			return QMainWindow.eventFilter(obj, event) #send it upwards

	def handleClick(self, pos): 
		""" record the mouse click at pos and show it"""
		print pos.x()
		print pos.y()
		self.corners.append(pos)
		#draw it
		painter=qg.QPainter(self.image) #FIXME: this gets garbage collected, right?
		painter.setPen(qc.Qt.red)
		painter.drawPoint(pos)
		self.display.setPixmap(self.image) #FIXME: this is bad code. maybe I need my own class
		#are we done yet?
		if len(self.corners) >= 4:
			self.finishConf()

	def finishConf(self): 
		""" 4 corners have been picked """
		self.label.setText("Thank you.")
		#TODO: actual calibration stuff
		self.display.removeEventFilter(self)
		self.conf.setEnabled(1)
		self.start.setEnabled(1)

	def slotStartEnd(self): 
		""" start/end a game """
		if self.inGame:
			self.pause.setEnabled(0) #it doesn't make sense to pause outside a game
			if self.paused:
				self.pause.setText("Pause")
				self.paused=0
			self.start.setText("Start")
			self.inGame=0
		else:
			self.pause.setEnabled(1)
			self.start.setText("End")
			self.inGame=1

	def slotPause(self): 
		""" pause/unpause game """
		if self.paused:
			self.pause.setText("Pause")
			self.paused=0
		else:
			self.pause.setText("Resume")
			self.paused=1


#generic qt startup code
if __name__ == "__main__":
	a      = qg.QApplication([])
	w      = GoUi()
	w.show()                                    # There is no setMainWidget(w)
	a.exec_()                                   # exec_loop() replaced by exec_()

